﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Entity;
using System.Runtime.InteropServices;

namespace PDAMain.Forms.InspectionWork
{
    public partial class InspectionDetail : BaseForm
    {
        public string assignmentId;
        private bool scanFlag = true;
        public InspectionDetail()
        {
            InitializeComponent();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            InspectionList list = new InspectionList();
            list.Show();
            this.Close();
        }

        private void InspectionDetail_Load(object sender, EventArgs e)
        {
            bindData();
        }

        //加载数据
        private void bindData()
        {
            //获取title数据
            assignmentId = DataHelper.InspectionWorkDao.AssignmentId;
            DataSet dsList = DataHelper.InspectionWorkDao.getDetail(assignmentId);
            bool Flag = false;
            if (dsList.Tables.Count == 0 || dsList.Tables[0].Rows.Count == 0)
            {
                Flag = true;
            }
            DataSet dsTaskList = DataHelper.InspectionWorkDao.getTaskDetail(assignmentId);
            if (dsTaskList.Tables.Count == 0 || dsTaskList.Tables[0].Rows.Count == 0)
            {
                Flag = true;
            }

           if (Flag)
            {
                dgworkList.TableStyles.Clear();
                dgworkList.Refresh();
                MessageBox.Show("没有巡检任务！");
                return;
            }

            DataTable dtTitle=dsList.Tables[0];
            DataTable dtTaskList=dsTaskList.Tables[0];
            //绑定表头数据
            
            lblWorkNo.Text = dtTitle.Rows[0]["WONUM"].ToString();
            lblDescription.Text = dtTitle.Rows[0]["description"].ToString();
            lblLocation.Text = dtTitle.Rows[0]["location"].ToString();
            lblLocDescription.Text = dtTitle.Rows[0]["LOCDESCRIPTION"].ToString();
            lblAssetNo.Text = dtTitle.Rows[0]["ASSETNUM"].ToString();
            lblAssetDescription.Text = dtTitle.Rows[0]["ASSDESCRIPTION"].ToString();
            lblStatus.Text = DataHelper.InspectionWorkDao.getShow(dtTitle.Rows[0]["STATUS"].ToString());
            lblTaskCount.Text = dtTaskList.Rows.Count.ToString();

            //绑定从数据
            dgworkList.DataSource = dtTaskList;
            DataGridTableStyle dts = new DataGridTableStyle();
            dts.MappingName = dtTaskList.TableName;

            DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
            col1.HeaderText = "任务编号";
            col1.MappingName = "TASKID";
            col1.Width = 80;
            dts.GridColumnStyles.Add(col1);

            DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
            col2.HeaderText = "任务描述";
            col2.MappingName = "DESCRIPTION";
            col2.Width = 234;
            dts.GridColumnStyles.Add(col2);

            dgworkList.TableStyles.Clear();
            dgworkList.TableStyles.Add(dts);
            dgworkList.Refresh();
        }

        private void saveData()
        {
            
            //巡检完成
            //1表示“处理中”，3表示“报修”，9表示“已完成”
            if (DataHelper.InspectionWorkDao.deal(assignmentId, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), ""))
            {
                MessageBox.Show("成功同步数据至远程数据库");
            }
            else
            {
                if (DataHelper.InspectionWorkDao.insertLacolTable(assignmentId, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), ""))
                {
                    MessageBox.Show("同步数据失败，保存至本地数据库");
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            backToList();
        }

        /*private void menuItem3_Click(object sender, EventArgs e)
        {
            // 关闭BarCode扫描设备
           // CloseBarCode();
            //报修
            InspectionToRepair repair = new InspectionToRepair();
            repair.assignmentId = assignmentId;
            repair.Show();
            this.Close();
            
        }*/

        private void menuItem4_Click(object sender, EventArgs e)
        {
            //巡检完成
          
            if (MessageBox.Show("你确定要执行此操作吗？", "提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                saveData();
            }
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            InspectionToFastRepair fastRepair = new InspectionToFastRepair();
            fastRepair.assignmentId = assignmentId;
            fastRepair.Show();
            this.Close();
            
        }

        private void InspectionDetail_Closed(object sender, EventArgs e)
        {
        }

        private void backToList()
        {
            InspectionList list = new InspectionList();
            list.Show();
            this.Close();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            InspectionToRepair repair = new InspectionToRepair();
            repair.assignmentId = assignmentId;
            repair.Show();
            this.Close();
        }
    }
}